package com.example.comiccenter.service.implt;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.CollectMapper;
import com.example.comiccenter.dao.ComicMapper;
import com.example.comiccenter.domain.Collect;
import com.example.comiccenter.domain.Comic;
import com.example.comiccenter.service.CollectService;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private ComicMapper comicMapper;
    @Override
    public ResultData getAllCollect(SearchBean<Collect> searchBean) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq(searchBean.getData().getUsername()!=null,"username",searchBean.getData().getUsername())
                .eq(searchBean.getData().getComicId()!=null,"comicId",searchBean.getData().getComicId());
        List<Collect> records = collectMapper.selectPage(new Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper).getRecords();
        ArrayList<Comic> comics = new ArrayList<>();
        for (Collect record : records) {
            Integer comicId = record.getComicId();
            Comic comic = comicMapper.selectById(comicId);
            comics.add(comic);
        }
        return ResultData.success("查询成功",comics);
    }

    @Override
    public ResultData addCollect(Collect collect) {
        if (collect.getUsername() == null || collect.getComicId() == null){
            return ResultData.error(400,"传入参数错误");
        }
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("username",collect.getUsername()).eq("comicId",collect.getComicId());
        if (collectMapper.selectCount(wrapper)>0) {
            return ResultData.success("该作品已收藏");
        }
        collect.setCollectTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        collectMapper.insert(collect);

        //添加收藏完后修改comic的收藏数量
        QueryWrapper<Comic> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id",collect.getComicId());
        Comic comic = comicMapper.selectOne(wrapper1);
        comic.setCollects(comic.getCollects()+1);
        comic.setHeat(comic.getHeat()+1);

        comicMapper.updateById(comic);

        return ResultData.success("添加收藏成功");
    }

    @Override
    public ResultData deleteCollect(Collect collect) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("username",collect.getUsername()).eq("comicId",collect.getComicId());
        collectMapper.delete(wrapper);

        //取消收藏完后修改comic的收藏数量
        QueryWrapper<Comic> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id",collect.getComicId());
        Comic comic = comicMapper.selectOne(wrapper1);
        comic.setCollects(comic.getCollects()-1);
        comic.setHeat(comic.getHeat()-1);
        comicMapper.updateById(comic);

        return ResultData.success("取消收藏成功");
    }
}
